package com.mingserve.raw.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.mingserve.common.core.domain.R;
import com.mingserve.common.core.utils.StringUtils;
import com.mingserve.raw.domain.*;
import com.mingserve.raw.service.IDeviceService;
import com.mingserve.raw.service.IEquipmentService;
import com.mingserve.raw.service.IPointService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 设备结构数据查询API
 *
 * @author lvwshuai
 * @date 2024-11-27
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/equip")
public class EquipmentController
{
    private final IEquipmentService equipmentService;
    private final IPointService pointService;
    private final IDeviceService deviceService;

    /**
     * 查询设备列表
     *
     * @return
     */
    @GetMapping("/queryEquip")
    public R<List<Equipment>> queryEquip()
    {
        List<Equipment> list = equipmentService.queryList();
        if (StringUtils.isNotEmpty(list)) {
            list = list.stream().filter(l -> l.getLevel() < 40).toList();
        }
        return R.ok(list);
    }

    /**
     * 查询设备详情
     *
     * @return
     */
    @GetMapping("/getEquipByCode")
    public R<Equipment> getEquipByCode(@RequestParam("equipmentCode")String equipmentCode)
    {
        Equipment equipment = equipmentService.getEquipmentByCode(equipmentCode);
        return R.ok(equipment);
    }

    /**
     * 查询设备配置详情
     *
     * @return
     */
    @GetMapping("/getEquipConfigByCode")
    public R<EquipmentConfig> getEquipConfigByCode(@RequestParam("equipmentCode") String equipmentCode)
    {
        EquipmentConfig config = equipmentService.getEquipmentConfigByCode(equipmentCode);
        return R.ok(config);
    }

    /**
     * 设备采集配置
     *
     * @param equipmentConfig
     * @return
     */
    @PostMapping("/updateEquipConfig")
    public R<Boolean> updateEquipConfig(@RequestBody EquipmentConfig equipmentConfig)
    {
        return R.ok(equipmentService.updateEquipmentConfigByCode(equipmentConfig));
    }

    /**
     * 设置是否开始采集
     * 边缘端：StartUseFlag=2 时采数
     *
     * @param map
     * @return
     */
    @PostMapping("/setStartUseFlag")
    public R<Boolean> setStartUseFlag(@RequestBody Map<String, Object> map)
    {
        EquipmentConfig find = equipmentService.getEquipmentConfigByCode(map.get("equipmentCode").toString());
        if (find == null) return R.fail("采集间隔未配置");
        JSONObject configMap = JSONObject.parseObject(find.getConfig());
        JSONObject config = Optional.ofNullable(configMap.getJSONObject("iphm_sensor_equipment")).orElse(new JSONObject());
        config.put("StartUseFlag", map.get("StartUseFlag"));
        configMap.put("iphm_sensor_equipment", config);
        EquipmentConfig equipmentConfig = new EquipmentConfig();
        equipmentConfig.setEquipmentCode(find.getEquipmentCode());
        equipmentConfig.setConfig(JSON.toJSONString(configMap));
        boolean flag = equipmentService.updateEquipmentConfigByCode(equipmentConfig);
        return R.ok(flag);
    }

    /**
     * 查询设备下测点列表
     *
     * @param equipment
     * @return
     */
    @GetMapping("/queryPoint")
    public R<List<Point>> queryPoint(Equipment equipment)
    {
        List<Point> list = pointService.queryByEquipment(equipment);
        return R.ok(list);
    }

    /**
     * 查询测点详情
     *
     * @param pointCode
     * @return
     */
    @GetMapping("/getPointByCode")
    public R<Point> getPointByCode(@RequestParam("pointCode")String pointCode)
    {
        Point point = pointService.getPointByCode(pointCode);
        return R.ok(point);
    }

    /**
     * 修改测点信息
     *
     * @param point
     * @return
     */
    @PostMapping("/updatePoint")
    public R<Integer> updatePoint(@RequestBody Point point)
    {
        int rows = pointService.updatePoint(point);
        return R.ok(rows);
    }

    /**
     * 查询传感器列表
     *
     * @return
     */
    @GetMapping("/queryDevice")
    public R<List<Device>> queryDevice()
    {
        List<Device> list = deviceService.queryList();
        return R.ok(list);
    }

    /**
     * 查询传感器详情
     *
     * @param deviceCode
     * @return
     */
    @GetMapping("/getDeviceByCode")
    public R<Device> getDeviceByCode(@RequestParam("deviceCode") String deviceCode)
    {
        Device device = deviceService.getDeviceByCode(deviceCode);
        return R.ok(device);
    }

    /**
     * 设置传感器采集配置
     *
     * @param device
     * @return
     */
    @PostMapping("/updateDevice")
    public R<Boolean> updateDevice(@RequestBody Device device)
    {
        return R.ok(deviceService.updateDevice(device));
    }

    /**
     * 查询测点故障配置
     *
     * @param pointCode
     * @return
     */
    @GetMapping("/getPointFaultByCode")
    public R<PointFault> getPointFaultByCode(@RequestParam("pointCode")String pointCode)
    {
        return R.ok(pointService.getPointFaultByCode(pointCode));
    }
}